package com.zzyy.study.config;

import com.github.benmanes.caffeine.cache.Cache;
import io.lettuce.core.support.caching.CacheAccessor;
import lombok.extern.slf4j.Slf4j;

/**
 * @auther zzyy
 * @create 2024-01-24 18:32
 */
@Slf4j
public class CaffeineCacheAccessor implements CacheAccessor
{
    private Cache localCacheCaffeine;

    public CaffeineCacheAccessor(Cache cache)
    {
        this.localCacheCaffeine = cache;
    }

    @Override
    public Object get(Object key)
    {
        log.info("caffeine get:{}",key);
        return localCacheCaffeine.getIfPresent(key);
    }

    @Override
    public void put(Object key, Object value)
    {
        log.info("caffeine put:{},{}",key,value);
        localCacheCaffeine.put(key,value);
    }

    @Override
    public void evict(Object key)
    {
        log.info("caffeine evict:{}",key);
        localCacheCaffeine.invalidate(key);
    }
}
